﻿using MediatR;
using ProcurementManagement.Applcation.Command.PurchaseContractsCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Dtos;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace ProcurementManagement.Applcation.CommandHanlers.PurchaseContractsCommandHandler
{
    public class PurchaseContractsSelectCommandHandler : IRequestHandler<PurchaseContractsSelectCommand, PageDTO<SelectpurchaseContractDTO>>
    {
        private readonly InterfaceVendorRepository _vendorRep;
        private readonly IPurchaseContractsRepository _purchaseContractsRep;
        private readonly ISourcProductContractRepository _sourceProductContractRep;
        ISqlSugarClient db;

        public PurchaseContractsSelectCommandHandler(InterfaceVendorRepository repository, IPurchaseContractsRepository purchaseContractsRep, ISourcProductContractRepository sourceProductContractRep, ISqlSugarClient db)
        {
            this._vendorRep = repository;
            this._purchaseContractsRep = purchaseContractsRep;
            this._sourceProductContractRep = sourceProductContractRep;
            this.db = db;
        }

        public async Task<PageDTO<SelectpurchaseContractDTO>> Handle(PurchaseContractsSelectCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<PurchaseContracts, SourcProductContract, Vendor>((purchaseContracts, sourceProductContract, vendor) => new JoinQueryInfos(
                JoinType.Inner, purchaseContracts.PCNumber == sourceProductContract.PCNumber,
                JoinType.Inner, purchaseContracts.VendorId == vendor.VendorId
                ))
                .Where((purchaseContracts, sourceProductContract, vendor) => purchaseContracts.IsDel==false)
                .Select((purchaseContracts, sourceProductContract, vendor) => new SelectpurchaseContractDTO
                {
                    PurchaseContractsIds = purchaseContracts.PurchaseContractsIds,
                    PCName=purchaseContracts.PCName,
                    VendorId = purchaseContracts.VendorId,
                    PCNumber = purchaseContracts.PCNumber,
                    PCTypes = purchaseContracts.PCTypes,
                    PCWhetherQuality = purchaseContracts.PCWhetherQuality,
                    ManpowerId = purchaseContracts.ManpowerId,
                    PCOurRepresentatives = purchaseContracts.PCOurRepresentatives,
                    PCRepresentParty = purchaseContracts.PCRepresentParty,
                    ProcureApplyId = purchaseContracts.ProcureApplyId,
                    PContractsDate = purchaseContracts.PContractsDate,
                    PCPreparationDate = purchaseContracts.PCPreparationDate,
                    PCProductPrice = purchaseContracts.PCProductPrice,
                    PCPreferentialPrice = purchaseContracts.PCPreferentialPrice,
                    PCAfterOfferPrice = purchaseContracts.PCAfterOfferPrice,
                    SourcProductContractId = sourceProductContract.SourcProductContractId,
                    ProductId = sourceProductContract.ProductId,
                    SPCNum = sourceProductContract.SPCNum,
                    SPCPrice = sourceProductContract.SPCPrice,
                    SPCDocumentsType = sourceProductContract.SPCDocumentsType,
                    SPCTotalPrice = sourceProductContract.SPCTotalPrice,
                    SPCAfterPrice = sourceProductContract.SPCAfterPrice,
                    SPCAfterTotalPrice = sourceProductContract.SPCAfterTotalPrice,
                    SPCArrivalDate = sourceProductContract.SPCArrivalDate,
                    SPCRemark = sourceProductContract.SPCRemark,
                    SPPName=sourceProductContract.SPPName,
                    SPPSpec = sourceProductContract.SPPSpec,
                    SPPNumber=sourceProductContract.SPPNumber,
                    AddName = sourceProductContract.AddName,
                    AddTime = sourceProductContract.AddTime,
                    UpdName = sourceProductContract.UpdName,
                    UpdTime = sourceProductContract.UpdTime,
                    DelName = sourceProductContract.DelName,
                    DelTime = sourceProductContract.DelTime,
                    IsDel = sourceProductContract.IsDel,
                    Office_Id = sourceProductContract.Office_Id,
                    User_Id = sourceProductContract.User_Id,
                    VName = vendor.VName,
                });
            if (!string.IsNullOrEmpty(request.PCNumber))
            {
                list = list.Where(purchaseContracts => purchaseContracts.PCNumber.Contains(request.PCNumber));
            }
            if (!string.IsNullOrEmpty(request.PContractsDate1))
            {
                var date1 = DateTime.Parse(request.PContractsDate1);
                list = list.Where(purchaseContracts => purchaseContracts.PContractsDate >= date1);
            }
            if (!string.IsNullOrEmpty(request.PContractsDate2))
            {
                var date2 = DateTime.Parse(request.PContractsDate2);
                list = list.Where(purchaseContracts => purchaseContracts.PContractsDate <= date2);
            }
            if (request.PCOurRepresentatives > 0)
            {
                list = list.Where(purchaseContracts => purchaseContracts.PCOurRepresentatives == request.PCOurRepresentatives);
            }
            if (request.VendorId > 0)
            {
                list = list.Where(purchaseContracts => purchaseContracts.VendorId == request.VendorId);
            }
            // 执行查询并获取总数  
            var totalCount = await list.CountAsync();

            // 应用分页  
            var sourceProductContracts = await list
                .OrderByDescending(purchaseContracts => purchaseContracts.PurchaseContractsIds)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToListAsync();

            // 准备结果  
            var pages = new PageDTO<SelectpurchaseContractDTO>
            {
                Totalcount = totalCount,
                Pagecount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize),
                list = sourceProductContracts
            };

            return pages;
        }
    }
}
